import Vue from 'vue'
import VueRouter from "vue-router"
import store from "@/store"

//使用路由懒加载
const Home = ()=>import("@pages/Home")
const Search = ()=>import("@pages/Search")
const Login = ()=>import("@pages/Login")
const Register = ()=>import("@pages/Register")
const Detail = ()=>import("@pages/Detail")
const AddCartSuccess = ()=>import("@pages/AddCartSuccess")
const ShopCart =()=>import ("@pages/ShopCart")
const Pay = () => import("@pages/Pay");
const PaySuccess = () => import("@pages/PaySuccess");
const Trade = () => import("@pages/Trade");
const Center = () => import("@pages/Center");
const MyOrder = () => import("@pages/Center/MyOrder");
const TeamOrder = () => import("@pages/Center/TeamOrder");


Vue.use(VueRouter)

const lastPush = VueRouter.prototype.push

VueRouter.prototype.push = function(
    loaction,
    onComplete=()=>{},
    onAbort=()=>{}
    ){
    return lastPush.call(this,loaction,onComplete,onAbort)
}

const router =  new VueRouter({
  scrollBehavior(){
    return{
      x:0,
      y:0
    }
  },
    mode:"history",
    routes:[
        {
            path: "/home",
            component: Home,
            name: "home",
          },
          {
            path: "/search/:keyword?",
            component: Search,
            name: "search",
          },
          {
            path: "/login",
            component: Login,
            name: "login",
            //meta的值只能在对应的 组件打印出来
            meta:{isLoginhidden:true},

            //路由独享 在进路由时就会判断 如果有token 不让你进去登陆页面
            beforeEnter(to,from,next){
              const token = store.state.user.token;
              if(token){
                next("/")
              }else{
                next()
              }
            }
          },
          {
            path: "/register",
            component: Register,
            name: "register",
            meta:{isLoginhidden:true}
          },
          {
            path: "/detail/:skuId",
            component: Detail,
            name: "detail",
          },
          {
            path: "/addCartSuccess",
            component: AddCartSuccess,
            name: "addCartSuccess",
            //路由独享守卫
            beforeEnter(to,from,next){
              if(sessionStorage.getItem("cartData")){
                next()
              }else{
                next("/shopcart")
              }
            }
          },
          {
            path: "/shopcart",
            component: ShopCart,
            name: "shopcart",
          },
          {
            path: "/pay",
            component: Pay,
            name: "pay",
            beforeEnter(to, from, next) {
              if (from.path.startsWith("/trade")) {
                next();
              } else {
                next("/shopcart");
              }
            },
          },
          {
            path: "/paysuccess",
            component: PaySuccess,
            name: "paysuccess",
            beforeEnter(to, from, next) {
              if (from.path.startsWith("/pay")) {
                next();
              } else {
                next("/shopcart");
              }
            },
          },
          {
            path: "/trade",
            component: Trade,
            name: "trade",
            beforeEnter(to, from, next) {
              if (from.path.startsWith("/shopcart")) {
                next();
              } else {
                next("/shopcart");
              }
            },
          },
          {
            path: "/center",
            component: Center,
            name: "center",
            children:[
              {
                path:"myorder",
                component:MyOrder,
                name:"myorder"
              },
              {
                path:"teamorder",
                component:TeamOrder,
                name:"teamorder"
              },
              {
                path:"",
                redirect:"/center/myorder"

              }
            ]
          },
          {
            path: "/",
            redirect: "/home",
          },
    ]

})

router.beforeEach((to,from,next)=>{
  const checkPath =["/center", "/pay", "/paysuccess", "/trade", "/shopcart"]
  //得到当前to的地址是否存在checkpath中
  const isCheckPath = checkPath.some(item=>{
    return to.path.startsWith(item)
  })
  //获取用户名
  const token = store.state.user.token;
  const nickName = store.state.user.nickName;
  if(nickName){
    return next()
  }
  if(isCheckPath){
    if(token){
      store.dispatch("user/getUserName");
      next();
    }else{
      next("/login")
    }
  }else{
    if(token){
      store.dispatch("user/getUserName").catch(()=>{
        store.state.user.token = ""
      });
    }
    next(); 
  }
})
export default router